package com.woniuxy.bookmallmanagersrever.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.bookmallmanagersrever.entity.TRole;
import com.woniuxy.bookmallmanagersrever.entity.TUrlPermission;
import com.woniuxy.bookmallmanagersrever.service.RoleService;
import com.woniuxy.bookmallmanagersrever.vo.ResponseResult;
import com.woniuxy.bookmallmanagersrever.vo.RolePerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class RoleController {

    @Autowired
    RoleService roleService ;

    @GetMapping("/role/list")
    public ResponseResult<PageInfo<TRole>> list(
            @RequestParam(value = "now",defaultValue = "1") Integer now,
            @RequestParam(value = "size",defaultValue = "3") Integer size,
            String name){
        ResponseResult<PageInfo<TRole>> result = new ResponseResult<>();
        try {
            PageInfo<TRole> lists = roleService.findRoleByPage(now,size,name);
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(lists);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result ;
    }

    @PostMapping("/role/add")
    public ResponseResult<Void> add(@RequestBody TRole role){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            roleService.add(role);
            result.setCode(200);
            result.setMsg("添加成功!");
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("添加失败!");
        }
        return result ;
    }

    @PutMapping("/role/modify")
    public ResponseResult<Void> modify(@RequestBody TRole role){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            roleService.modify(role);
            result.setCode(200);
            result.setMsg("修改成功!");
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("修改失败!");
        }
        return result ;
    }

    @DeleteMapping("/role/del/{id}")
    public ResponseResult<Void> removeByKey(@PathVariable("id") int id){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            roleService.removeByKey(id);
            result.setCode(200);
            result.setMsg("删除成功!");
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("删除失败!");
        }
        return result ;
    }

    @PostMapping("/role/addPerToRole")
    public ResponseResult<Void> addPerToRole(@RequestBody RolePerVo rolePerVo){
        ResponseResult<Void> result = new ResponseResult<>();
        try {
            roleService.addGrante(rolePerVo);
            result.setCode(200);
            result.setMsg("添加成功!");
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("添加失败!");
        }
        return result ;
    }

    @GetMapping("/role/getAllRoles")
    public ResponseResult<List<TRole>> getAllRoles(){
        ResponseResult<List<TRole>> result = new ResponseResult<>();
        try {
            List<TRole> lists = roleService.findAllRoles();
            result.setCode(200);
            result.setMsg("查询成功!");
            result.setData(lists);
        }catch (Exception e) {
            e.printStackTrace();
            result.setCode(201);
            result.setMsg("查询失败!");
        }
        return result ;
    }

}
